package cn.nfwx.tv.controller;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

import cn.nfwx.tv.R;
import cn.nfwx.tv.adapter.CompanyAdapter;
import cn.nfwx.tv.adapter.CompanyVideoAdapter;
import cn.nfwx.tv.adapter.HotVideoAdapter;
import cn.nfwx.tv.base.BaseActivity;
import cn.nfwx.tv.base.BaseDialogFragment;
import cn.nfwx.tv.fragment.callback.ActionCallback;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.helper.DialogFragmentHelper;
import cn.nfwx.tv.model.Company;
import cn.nfwx.tv.model.CompanyVideo;
import cn.nfwx.tv.util.RecyclerViewUtil;
import cn.nfwx.tv.widget.LoadView;
import me.everything.android.ui.overscroll.OverScrollDecoratorHelper;

public class TVCompanyViewController extends BaseDialogFragment implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener {

    private final static String KEY_COMPANY = "KEY_COMPANY";

    private RecyclerView rcv_company, rcv_company_video;
    private CompanyAdapter companyAdapter;
    private CompanyVideoAdapter hotVideoAdapter;

    private ArrayList<Company> companies;

    //private OnTvCompanyListener onTvCompanyListener;
    private ActionCallback mActionCallback;
    public final static String ACTION_COMPANY_DIMISS = "ACTION_COMPANY_DIMISS";
    public final static String ACTION_COMPANY_VIDEO = "ACTION_COMPANY_VIDEO";

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActionCallback = ActionCallback.NONE;
        if (activity instanceof ActionCallback) {
            mActionCallback = (ActionCallback) activity;
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if (adapter instanceof CompanyAdapter) {
            Company company = companyAdapter.getData().get(position);
            AccountHelper.getInstance().setCompanyName(company.getCompany());
            loadData(position);
        } else if (adapter instanceof CompanyVideoAdapter) {
            CompanyVideoAdapter mAdapter = (CompanyVideoAdapter) adapter;
            Parcelable parcelable = mAdapter.getData().get(position);
            if (parcelable instanceof CompanyVideo) {
                CompanyVideo video = (CompanyVideo) parcelable;
                if (mActionCallback != null) {
                    Bundle bundle = new Bundle();
                    bundle.putParcelable("data", video);
                    mActionCallback.callbackByfragment(ACTION_COMPANY_VIDEO, bundle);
                }
            }
        }
    }

    /**
     * dialog别名
     *
     * @return
     */
    public static String tag() {
        return "company";
    }

    /**
     * 销毁
     *
     * @param fragmentManager
     * @param tag
     */
    public static void dismiss(FragmentManager fragmentManager, String tag) {
        DialogFragmentHelper.removeFragment(fragmentManager, tag);
    }

    public interface OnTvCompanyListener {
        void onDialogDimiss();

        void onItemClick(CompanyVideo video);
    }

    public static TVCompanyViewController newInstance(ArrayList<Company> companies) {
        TVCompanyViewController fragment = new TVCompanyViewController();
        Bundle bundle = new Bundle();
        ArrayList<Company> companyList = new ArrayList<>();
        if (companies != null) {
            companyList.addAll(companies);
        }
        bundle.putParcelableArrayList(KEY_COMPANY, companyList);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.layout_company_video;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        List<Company> companyList = bundle.getParcelableArrayList(KEY_COMPANY);
        companies = new ArrayList<>(companyList);

        rcv_company = findView(R.id.rcv_company);
        rcv_company_video = findView(R.id.rcv_company_video);

        LoadView loadView = new LoadView(getContext());
        loadView.loadNodata();

        RecyclerViewUtil.setLinearLayoutManagerWithDivder(getContext(), rcv_company);
        RecyclerViewUtil.setGridLayoutManager(getContext(), rcv_company_video, 2);

        companyAdapter = new CompanyAdapter();
        rcv_company.setAdapter(companyAdapter);
        if (!companies.isEmpty()) {
            companyAdapter.setNewData(companies);
        }

        hotVideoAdapter = new CompanyVideoAdapter();
        rcv_company_video.setAdapter(hotVideoAdapter);
        hotVideoAdapter.addHeaderView(LayoutInflater.from(getContext()).inflate(R.layout.layout_view, null));
        hotVideoAdapter.addFooterView(LayoutInflater.from(getContext()).inflate(R.layout.layout_view, null));
        RecyclerView.ItemDecoration itemDecoration = new HorizontalDividerItemDecoration.Builder(getContext()).size(20).build();
        rcv_company_video.addItemDecoration(itemDecoration);
        hotVideoAdapter.setEmptyView(loadView);

        OverScrollDecoratorHelper.setUpOverScroll(rcv_company_video, OverScrollDecoratorHelper.ORIENTATION_VERTICAL);
        OverScrollDecoratorHelper.setUpOverScroll(rcv_company, OverScrollDecoratorHelper.ORIENTATION_VERTICAL);

        int k = 0;
        for (int i = 0; i < companies.size(); i++) {
            Company company = companies.get(i);
            if (company.getCompany().equals(AccountHelper.getInstance().getCompanyName())) {
                k = i;
                break;
            }
        }
        loadData(k);
        initListener();
    }

    /**
     * 加载数据
     *
     * @param position
     */
    private void loadData(int position) {
        if (companies.isEmpty()) {
            return;
        }
        for (Company company : companies) {
            company.setPress(false);
        }
        Company company = companies.get(position);
        company.setPress(true);
        companyAdapter.notifyDataSetChanged();
        List<Parcelable> parcelables = new ArrayList<>();
        if (company.getVideos() != null) {
            parcelables.addAll(company.getVideos());
        }
        hotVideoAdapter.setNewData(parcelables);
    }

    /**
     * 监听
     */
    private void initListener() {
        findView(R.id.back_btn).setOnClickListener(this);
        companyAdapter.setOnItemClickListener(this);
        hotVideoAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                if (mActionCallback != null) {
                    mActionCallback.callbackByfragment(ACTION_COMPANY_DIMISS, null);
                }
                dismissAllowingStateLoss();
                break;
        }
    }

    @Override
    protected boolean isMatchParent() {
        return true;
    }
}
